/*
 Contact jtable model implementation
 */
package addressBook.model;

import java.util.List;
import javax.swing.table.AbstractTableModel;

/**
 *
 * @author mathieubakrim
 */
public class ContactsTable extends AbstractTableModel{
    
    private  List<Contact> contacts;
    
    private String[] titles = {"lastName", "firstName", "phoneNumber", "addressPro", 
        "addressPer", "homePage", "emailPro", "emailPer", "group"}; 
    
    /**
     * ContactTable constructor
     * @param titles
     * @param contacts 
     */
    public ContactsTable(String[] titles, List<Contact> contacts ){
        super();
        this.titles = titles;         
        this.contacts = contacts;        
    }

    /**
     * Allow to make a cell editable or not
     * @param rowIndex
     * @param columnIndex
     * @return boolean isEditable
     */
    @Override
    public boolean isCellEditable(int rowIndex, int columnIndex){
         return true;
    }
        
    /**
     * Jtable overwritten row count get method
     * @return the number of the jtable rows
     */
    @Override
    public int getRowCount() {
        return contacts.size();
    }

    /**
     * Jtable overwritten col get method
     * @return the number of the jtable col
     */
    @Override
    public int getColumnCount() {
        return titles.length;
    }

    /**
     * Jtable get column name method
     * @param columnIndex
     * @return string column name
     */
    public String getColumName(int columnIndex){
        return titles[columnIndex];
    }

    /**
     * Get a jtable cell value
     * @param rowIndex
     * @param columnIndex
     * @return cell value
     */
    @Override
    public Object getValueAt(int rowIndex, int columnIndex) {
        switch(columnIndex){
            case 0:
                    return contacts.get(rowIndex).getLastName();
            case 1:
                    return contacts.get(rowIndex).getFirstName(); 
            case 2:
                    return contacts.get(rowIndex).getPhoneNumber();
            case 3:
                    return contacts.get(rowIndex).getAddressPro();
            case 4:
                    return contacts.get(rowIndex).getAddressPer();
            case 5:
                    return contacts.get(rowIndex).getHomePage();
            case 6:
                    return contacts.get(rowIndex).getEmailPro();
            case 7:
                    return contacts.get(rowIndex).getEmailPer();
            case 8: 
                    return contacts.get(rowIndex).getGroup();
            default:
                    return null;
        }
    }
    
    /**
     * Set a cell value
     * @param aValue
     * @param rowIndex
     * @param columnIndex 
     */
    @Override
    public void setValueAt(Object aValue, int rowIndex, int columnIndex) {
    if(aValue != null){
        Contact contact = contacts.get(rowIndex);
 
        switch(columnIndex){
            case 0:
                contact.setLastName((String)aValue);
            case 1:
                contact.setFirstName((String) aValue);
            case 2:
                contact.setPhoneNumber((String)aValue);
                break;
            case 3:
                contact.setAddressPro((String)aValue);
                break;
            case 4:
                contact.setAddressPer((String)aValue);
                break;
            case 5:
                contact.setHomePage((String)aValue);
                break;
            case 6:
                contact.setEmailPro((String)aValue);
                break;
            case 7:
                contact.setEmailPer((String)aValue);
                break;
            case 8:
                contact.setGroup((String)aValue);
        }
        fireTableCellUpdated(rowIndex, columnIndex);
    }
}

    /**
     * Update the jtable after a contact is created
     * @param contact
     */
    public void addContact(Contact contact){        
        fireTableRowsInserted(contacts.size()-1, contacts.size()-1);
    }
    
    /**
     * Update the jtable after a contact is removed/deleted
     * @param rowIndex 
     */
    public void removeContact(int rowIndex){
        contacts.remove(rowIndex);
        
        fireTableRowsDeleted(rowIndex, rowIndex);
    }
    
    //GETTERS
    public String[] getTitles() {
        return titles;
    }
    public List<Contact> getContacts() {
        return contacts;
    }    
}
